<?php
apf_require_controller('Oa_Attendance_Abstract');
apf_require_class('Tools_Uploader');
apf_require_class('Bll_Attendance_ApplyType');
apf_require_class('Bll_Attendance_Role');
apf_require_class('Bll_Attendance_UserKqStatus');
class Oa_Attendance_DelayController extends Oa_Attendance_AbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        $type = $params['type'] ? $params['type'] : 1;
        $from_date = $params['date'] ? $params['date'] : '';
        $this->request->set_attribute('from_date',$from_date);
        $type = intval($type);
        if($delay_type = Bll_Attendance_ApplyType::get_delay_type($type)) {
            $this->request->set_attribute('delay_type', $delay_type);
        }
        $user_id = $_SESSION['user_id'];

        $bll_role = new Bll_Attendance_Role();
        $r_user_id  =  $bll_role->get_sup_id($user_id,1);
        $audit_user =  $bll_role->get_sup_name($r_user_id);
        $data = array();
        //
        $typename = "";
        $start_time = '';
        $end_time = '';
        if($type==1){
            $start_time = '09:00';
            $end_time = '14:00';
        }elseif($type==2){
            $start_time = '14:00';
            $end_time = '18:00';
        }else{//这里是缺勤，找出当天的打卡记录

            apf_require_class('Const_LeaveType');
            $daily_bll = new Bll_Attendance_UserKqStatus();
            $leave_day = str_replace("-","",$from_date);
            $ac_status=$daily_bll->get_user_one_day_status($user_id,intval($leave_day));
            $start_time = '09:00';
            $end_time = '18:00';
            if($ac_status['am_status']==Const_LeaveType::KQ_AM_STATUS_3){
                $end_time = '14:00';
                $typename='上午缺勤';
            }
            if($ac_status['pm_status']==Const_LeaveType::KQ_PM_STATUS_4){
                $start_time = '14:00';
                $typename='下午缺勤';
            }
            if($ac_status['am_status']==Const_LeaveType::KQ_AM_STATUS_3 && $ac_status['pm_status']==Const_LeaveType::KQ_PM_STATUS_4){
                $typename = '全天缺勤';
                $start_time = '09:00';
                $end_time = '18:00';
            }
        }
        if($this->is_post_request()) {
            $is_up = false;
            if($_FILES['file']['tmp_name']) {
                $is_up = true;
                $upDir = 'upload/attendance';
                $uploader = new Tools_Uploader('file');
                $uploader->setValidExt(array(
                        '.jpg',
                        '.gif'
                ));
                $uploader->setUpDir(CODE_PATH . $upDir); // 需修改为你自己的路径
            }
            apf_require_class('Bll_Attendance_Apply');
            $bll_apply = new Bll_Attendance_Apply();
            $now = time();
            $leave_time=0;

            if($params['reason']=='忘记带卡'){
                $params['start_time']='09:00';
                $params['end_time']='18:00';
            }

            if($params['start_time']=='09:00' && $params['end_time']=='14:00'){
                $leave_time = 0.5;
            }elseif($params['start_time']=='14:00' && $params['end_time']=='18:00'){
                $leave_time = 0.5;
            }elseif($params['start_time']=='09:00' && $params['end_time']=='18:00'){
                $leave_time = 1;
            }

            $data = array(
                    'user_id' => $user_id,
                    'apply_time' => date("Y-m-d H:i:s", $now),
                    'apply_time_ext' => $now,
                    'start_time' => $params['now_date'].' '.$params['start_time'],
                    'end_time' => $params['now_date'].' '.$params['end_time'],
                    'leave_time' => $leave_time,
                    'type' => $params['delay_type'],
                    'leave_reason' => $params['reason'],
                    'leave_desc' => $params['desc'],
                    'status' => '未审核',
                    'r_user_id' => $r_user_id,
                    'last_touch' => date("Y-m-d H:i:s"),
            );
            if($data['leave_reason'] == '地铁故障') {
                $data['status'] = '已批准';
            }
            $is_submit = true;
            if($is_up) {
                if($uploader->upload()) {
                    $data['img'] = $uploader->getFileName();
                } else {
                    echo "<script type='text/javascript'>alert('"
                        . $uploader->getErrorMsg()
                        . "')</script>";
                    $is_submit = false;
                }
            }
            if($params['reason'] == '其它' && empty($params['desc'])) {
                echo "<script type='text/javascript'>alert('原因为其它时，必须填写Note!')</script>";
                $is_submit =  false;
            }
            if($params['delay_type'] == '缺勤' && $params['reason'] == '忘记带卡' && $this->_is_have_record($data)){
                echo "<script type='text/javascript'>alert('您有打卡记录，{$typename}的原因不能是“忘记带卡“')</script>";
                $is_submit =  false;
            }
            if(!$bll_apply->is_ever_apply($data)){
                echo "<script type='text/javascript'>alert('您在{$data['start_time']} {$data['end_time']}的“{$data['type']}“ 原因已经提交')</script>";
                $is_submit =  false;
            }
            if($is_submit) {
                $bll_apply->apply($data);
                echo "<script type='text/javascript'>alert('申请成功');document.location='".Tools_OAUrls::get_instance()->build_myapply_url()."'</script>";
            }
        }
        $this->request->set_attribute('typename', $typename);
        $this->request->set_attribute('start_time', $start_time);
        $this->request->set_attribute('end_time', $end_time);
        $this->request->set_attribute('audit_user',$audit_user);
        return 'Oa_Attendance_Delay';
    }

     /**
     * 缺勤情况下，是否有打卡记录
     * @param unknown_type $date
     */
    private function _is_have_record($date){
        $boo = false;
        apf_require_class('Bll_Attendance_AccessDayRecord');
        $bll = new Bll_Attendance_AccessDayRecord();
        $start_date = date('Ymd',strtotime($date['start_time']));
        $res = $bll->get_record_list($date['user_id'],$start_date,$start_date);
        if(!empty($res)){
            $boo = true;
        }
        return $boo;
    }

    /**
     * 查看是否有该异常状态
     * @param unknown_type $user_id
     * @param unknown_type $date
     * @param unknown_type $type
     * @return multitype:number string
     *
     */
    private function get_record_status($user_id,$date,$type){
        $result = array();
        apf_require_class('Bll_Attendance_DailyLeave');
        apf_require_class('Const_LeaveType');
        $bll = new Bll_Attendance_DailyLeave();
        $date = date('Ymd',strtotime($date));
        $res = $bll->find_kq_by_dateline($user_id, $date);
        if(!empty($res)){
            //迟到,早退
            if($res['am_status'] == Const_LeaveType::KQ_AM_STATUS_1 && $res['pm_status'] == Const_LeaveType::KQ_PM_STATUS_2 && $type == 4){
                $result['type'] = 4;
            }//迟到
            else if($res['am_status'] == Const_LeaveType::KQ_AM_STATUS_1 && $type ==1 ){
                $result['type'] = 1;
            }//早退
            else if($res['pm_status'] == Const_LeaveType::KQ_PM_STATUS_2 && $type ==2 ){
                $result['type'] = 2;
            }else if($type == 3){
                $result['type'] = 3;
                if($res['am_status'] == Const_LeaveType::KQ_AM_STATUS_3 && $res['pm_status'] == Const_LeaveType::KQ_PM_STATUS_4){
                    $result['status'] = "全天缺勤";
                }
                else if($res['am_status'] == Const_LeaveType::KQ_AM_STATUS_3){
                    $result['status'] = "上午缺勤";
                }
                else if($res['pm_status'] == Const_LeaveType::KQ_PM_STATUS_4){
                    $result['status'] = "下午缺勤";
                }
            }
        }else{
            $result['type'] = 3;
            $result['status'] = '全天缺勤';
        }
        return $result;
    }
}
